package edu.cebanc.spring.biblioteca.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.cebanc.spring.biblioteca.bean.UserBean;
import edu.cebanc.spring.biblioteca.dao.UsersDAO;
import edu.cebanc.spring.biblioteca.util.DataParser;

@Service("usersService")
@Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
public class UsersServiceImpl implements UsersService {
	
	@Autowired
	private UsersDAO usersDAO;

	@Override
	@Transactional(propagation= Propagation.REQUIRED, readOnly = false)
	public void saveUser(UserBean user) {
		//System.out.println(user.toString());
		DataParser parser = new DataParser();
		usersDAO.saveUser(parser.UserBean2User(user));
	}

	@Override
	public List<UserBean> listUsers() {
		DataParser parser = new DataParser();
		return parser.UserList2UserBeanList(usersDAO.listUsers());
	}

	@Override
	public UserBean loadUser(String id) {
		DataParser parser = new DataParser();
		return parser.User2UserBean(usersDAO.getUser(Integer.parseInt(id)));
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void deleteUser(String id) {
		usersDAO.delUser(usersDAO.getUser(Integer.parseInt(id)));
	}

}
